/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package persistence;

import entityBeans.Ownerinstitution;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.Query;
import javax.persistence.EntityNotFoundException;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;
import entityBeans.Recordlevelelement;
import java.util.ArrayList;
import java.util.List;
import persistence.exceptions.NonexistentEntityException;

/**
 *
 * @author allankv
 */
public class OwnerinstitutionJpaController {

    public OwnerinstitutionJpaController() {
        emf = Persistence.createEntityManagerFactory("bdd-javaPU");
    }
    private EntityManagerFactory emf = null;

    public EntityManager getEntityManager() {
        return emf.createEntityManager();
    }

    public Ownerinstitution save(Ownerinstitution ownerinstitution) {
        Ownerinstitution aux = findOwnerinstitution(ownerinstitution.getOwnerinstitution());
        if(aux==null)
            return create(ownerinstitution);
        else
            return aux;
    }

    public Ownerinstitution create(Ownerinstitution ownerinstitution) {
        Ownerinstitution r = null;
        if (ownerinstitution.getRecordlevelelementList() == null) {
            ownerinstitution.setRecordlevelelementList(new ArrayList<Recordlevelelement>());
        }
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            List<Recordlevelelement> attachedRecordlevelelementList = new ArrayList<Recordlevelelement>();
            for (Recordlevelelement recordlevelelementListRecordlevelelementToAttach : ownerinstitution.getRecordlevelelementList()) {
                recordlevelelementListRecordlevelelementToAttach = em.getReference(recordlevelelementListRecordlevelelementToAttach.getClass(), recordlevelelementListRecordlevelelementToAttach.getIdrecordlevelelement());
                attachedRecordlevelelementList.add(recordlevelelementListRecordlevelelementToAttach);
            }
            ownerinstitution.setRecordlevelelementList(attachedRecordlevelelementList);
            r = em.merge(ownerinstitution);
            for (Recordlevelelement recordlevelelementListRecordlevelelement : ownerinstitution.getRecordlevelelementList()) {
                Ownerinstitution oldOwnerinstitutionOfRecordlevelelementListRecordlevelelement = recordlevelelementListRecordlevelelement.getOwnerinstitution();
                recordlevelelementListRecordlevelelement.setOwnerinstitution(ownerinstitution);
                recordlevelelementListRecordlevelelement = em.merge(recordlevelelementListRecordlevelelement);
                if (oldOwnerinstitutionOfRecordlevelelementListRecordlevelelement != null) {
                    oldOwnerinstitutionOfRecordlevelelementListRecordlevelelement.getRecordlevelelementList().remove(recordlevelelementListRecordlevelelement);
                    oldOwnerinstitutionOfRecordlevelelementListRecordlevelelement = em.merge(oldOwnerinstitutionOfRecordlevelelementListRecordlevelelement);
                }
            }
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
        return r;
    }

    public void edit(Ownerinstitution ownerinstitution) throws NonexistentEntityException, Exception {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Ownerinstitution persistentOwnerinstitution = em.find(Ownerinstitution.class, ownerinstitution.getIdownerinstitution());
            List<Recordlevelelement> recordlevelelementListOld = persistentOwnerinstitution.getRecordlevelelementList();
            List<Recordlevelelement> recordlevelelementListNew = ownerinstitution.getRecordlevelelementList();
            List<Recordlevelelement> attachedRecordlevelelementListNew = new ArrayList<Recordlevelelement>();
            for (Recordlevelelement recordlevelelementListNewRecordlevelelementToAttach : recordlevelelementListNew) {
                recordlevelelementListNewRecordlevelelementToAttach = em.getReference(recordlevelelementListNewRecordlevelelementToAttach.getClass(), recordlevelelementListNewRecordlevelelementToAttach.getIdrecordlevelelement());
                attachedRecordlevelelementListNew.add(recordlevelelementListNewRecordlevelelementToAttach);
            }
            recordlevelelementListNew = attachedRecordlevelelementListNew;
            ownerinstitution.setRecordlevelelementList(recordlevelelementListNew);
            ownerinstitution = em.merge(ownerinstitution);
            for (Recordlevelelement recordlevelelementListOldRecordlevelelement : recordlevelelementListOld) {
                if (!recordlevelelementListNew.contains(recordlevelelementListOldRecordlevelelement)) {
                    recordlevelelementListOldRecordlevelelement.setOwnerinstitution(null);
                    recordlevelelementListOldRecordlevelelement = em.merge(recordlevelelementListOldRecordlevelelement);
                }
            }
            for (Recordlevelelement recordlevelelementListNewRecordlevelelement : recordlevelelementListNew) {
                if (!recordlevelelementListOld.contains(recordlevelelementListNewRecordlevelelement)) {
                    Ownerinstitution oldOwnerinstitutionOfRecordlevelelementListNewRecordlevelelement = recordlevelelementListNewRecordlevelelement.getOwnerinstitution();
                    recordlevelelementListNewRecordlevelelement.setOwnerinstitution(ownerinstitution);
                    recordlevelelementListNewRecordlevelelement = em.merge(recordlevelelementListNewRecordlevelelement);
                    if (oldOwnerinstitutionOfRecordlevelelementListNewRecordlevelelement != null && !oldOwnerinstitutionOfRecordlevelelementListNewRecordlevelelement.equals(ownerinstitution)) {
                        oldOwnerinstitutionOfRecordlevelelementListNewRecordlevelelement.getRecordlevelelementList().remove(recordlevelelementListNewRecordlevelelement);
                        oldOwnerinstitutionOfRecordlevelelementListNewRecordlevelelement = em.merge(oldOwnerinstitutionOfRecordlevelelementListNewRecordlevelelement);
                    }
                }
            }
            em.getTransaction().commit();
        } catch (Exception ex) {
            String msg = ex.getLocalizedMessage();
            if (msg == null || msg.length() == 0) {
                Integer id = ownerinstitution.getIdownerinstitution();
                if (findOwnerinstitution(id) == null) {
                    throw new NonexistentEntityException("The ownerinstitution with id " + id + " no longer exists.");
                }
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void destroy(Integer id) throws NonexistentEntityException {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Ownerinstitution ownerinstitution;
            try {
                ownerinstitution = em.getReference(Ownerinstitution.class, id);
                ownerinstitution.getIdownerinstitution();
            } catch (EntityNotFoundException enfe) {
                throw new NonexistentEntityException("The ownerinstitution with id " + id + " no longer exists.", enfe);
            }
            List<Recordlevelelement> recordlevelelementList = ownerinstitution.getRecordlevelelementList();
            for (Recordlevelelement recordlevelelementListRecordlevelelement : recordlevelelementList) {
                recordlevelelementListRecordlevelelement.setOwnerinstitution(null);
                recordlevelelementListRecordlevelelement = em.merge(recordlevelelementListRecordlevelelement);
            }
            em.remove(ownerinstitution);
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public List<Ownerinstitution> findOwnerinstitutionEntities() {
        return findOwnerinstitutionEntities(true, -1, -1);
    }

    public List<Ownerinstitution> findOwnerinstitutionEntities(int maxResults, int firstResult) {
        return findOwnerinstitutionEntities(false, maxResults, firstResult);
    }

    private List<Ownerinstitution> findOwnerinstitutionEntities(boolean all, int maxResults, int firstResult) {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            cq.select(cq.from(Ownerinstitution.class));
            Query q = em.createQuery(cq);
            if (!all) {
                q.setMaxResults(maxResults);
                q.setFirstResult(firstResult);
            }
            return q.getResultList();
        } finally {
            em.close();
        }
    }
    public Ownerinstitution findOwnerinstitution(String q) {
        Query query= getEntityManager().createNamedQuery("Ownerinstitution.findByOwnerinstitution");
        query.setParameter("ownerinstitution",q);

        return query.getResultList().isEmpty()?null:(Ownerinstitution)query.getResultList().get(0);
    }
    public Ownerinstitution findOwnerinstitution(Integer id) {
        EntityManager em = getEntityManager();
        try {
            return em.find(Ownerinstitution.class, id);
        } finally {
            em.close();
        }
    }

    public int getOwnerinstitutionCount() {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            Root<Ownerinstitution> rt = cq.from(Ownerinstitution.class);
            cq.select(em.getCriteriaBuilder().count(rt));
            Query q = em.createQuery(cq);
            return ((Long) q.getSingleResult()).intValue();
        } finally {
            em.close();
        }
    }

}
